/*
 * Inspired by http://www.antonioshome.net/kitchen/swingnbrcp/swingnbrcp-nodes2.php
 */
package org.tintuna.cca.node;

import java.util.List;
import org.openide.nodes.ChildFactory;
import org.tintuna.cca.ability.Reloadable;
import org.tintuna.cca.data.Row;
import org.tintuna.cca.data.Spreadsheet;

/**
 *
 * @author bsmith
 */
public class SpreadsheetNodeChildFactory extends ChildFactory<Row> {

    private Spreadsheet spreadsheet;

    SpreadsheetNodeChildFactory(Spreadsheet spreadsheet) {
        this.spreadsheet = spreadsheet;
    }

    @Override
    protected boolean createKeys(List<Row> row) {
        // Retrieve the "Reloadable" ability from the query object
        // (now, abilities are cool, aren't they?)
        Reloadable r = spreadsheet.getLookup().lookup(Reloadable.class);

        if (r != null) {
            try {
                r.reload();
            } catch (Exception e) {
                // Empty
            }
        }

        // Add all the tweets in the query to the list
        row.addAll(spreadsheet.getRows());
        // Indicate that we're done
        return true;
    }

    @Override
  protected RowNode createNodeForKey(Row key) {
    return new RowNode(key);
  }
}
